package com.zzyl.controller;

import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.NursingLevelDto;
import com.zzyl.service.NursingLevelService;
import com.zzyl.vo.NursingLevelVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.javassist.NotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/nursingLevel")
@Api(tags = "护理等级管理")
@Slf4j
public class NursingLevelController extends BaseController {

    @Autowired
    private NursingLevelService nursingLevelService;

    @GetMapping("/listAll")
    @ApiOperation("查询所有护理等级")
    public ResponseResult<List<NursingLevelVo>> getAll() {
        try {
            List<NursingLevelVo> list = nursingLevelService.getAll();
            return ResponseResult.success(list);
        } catch (Exception e) {
            log.error("查询所有护理等级", e);
            return ResponseResult.error("查询失败");
        }
    }

    @GetMapping("/listByPage")
    @ApiOperation("分页查询护理等级列表")
    public ResponseResult<PageResponse<NursingLevelVo>> getByPage(
            @ApiParam(value = "护理等级名称")
            @RequestParam(value = "name", required = false) String name,
            @ApiParam(value = "状态：0-禁用，1-启用")
            @RequestParam(value = "status", required = false) Integer status,
            @ApiParam(value = "当前页码")
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @ApiParam(value = "每页显示数量")
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        try {
            PageResponse<NursingLevelVo> nursingLevelPageInfo = nursingLevelService.getByPage(name, status, pageNum, pageSize);
            return ResponseResult.success(nursingLevelPageInfo);
        } catch (Exception e) {
            log.error("分页查询护理等级失败", e);
            return ResponseResult.error("查询失败：" + e.getMessage());
        }
    }

    @PostMapping("/insert")
    @ApiOperation("新增护理等级")
    public ResponseResult add(
            @ApiParam(value = "护理等级数据传输对象", required = true)
            @Validated @RequestBody NursingLevelDto nursingLevelDTO) {
        nursingLevelService.add(nursingLevelDTO);
        return ResponseResult.success("新增成功");
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id查询护理等级")
    public ResponseResult<NursingLevelVo> get(@PathVariable("id") Integer id) {
        try {
            NursingLevelVo nursingLevelVo = nursingLevelService.getById(id);
            return ResponseResult.success(nursingLevelVo);
        } catch (Exception e) {
            log.error("查询护理等级失败", e);
            return ResponseResult.error("查询失败：" + e.getMessage());
        }
    }

    @PutMapping("update")
    @ApiOperation("更改护理等级")
    public ResponseResult update(
            @ApiParam(value = "护理等级数据传输对象", required = true)
            @RequestBody NursingLevelDto nursingLevelDTO) {
        nursingLevelService.update(nursingLevelDTO);
        return ResponseResult.success("修改成功");
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation("根据id删除护理等级")
    public ResponseResult delete(@PathVariable("id") Long id) {
        try {
            nursingLevelService.deleteById(id);
            return ResponseResult.success("删除成功");
        } catch (NotFoundException e) {
            log.warn("删除失败：{}", e.getMessage());
            return ResponseResult.error(e.getMessage());
        } catch (Exception e) {
            log.error("删除失败", e);
            return ResponseResult.error("删除失败");
        }
    }

    @PutMapping("/{id}/status/{status}")
    @ApiOperation("启用禁用")
    public ResponseResult updateStatus(@PathVariable("id") Long id, @PathVariable("status") Integer status) {
        try {
            nursingLevelService.updateStatus(id, status);
            String message = status == 1 ? "已启用" : "已禁用";
            return ResponseResult.success(message);
        } catch (Exception e) {
            log.error("状态更新失败", e);
            String errorMessage = status == 1 ? "启用失败" : "禁用失败";
            return ResponseResult.error(errorMessage);
        }
    }

}
